package com.example.demo.controller;

import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户控制器
 * @author 系统生成
 */
// @RestController：等价于@Controller + @ResponseBody（返回JSON）
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    // 欢迎页面
    @GetMapping("/")
    public Map<String, Object> welcome() {
        Map<String, Object> response = new HashMap<>();
        response.put("message", "欢迎使用 demo2 Spring Boot 全注解配置项目");
        response.put("version", "1.0.0");
        response.put("apis", Map.of(
                "POST /users", "创建用户",
                "GET /users/{username}", "查询用户",
                "GET /h2-console", "H2数据库控制台"
        ));
        return response;
    }

    // POST请求：创建用户
    @PostMapping("/users")
    public User createUser(@RequestBody User user) {
        return userService.saveUser(user);
    }

    // GET请求：查询用户
    @GetMapping("/users/{username}")
    public User getUser(@PathVariable String username) {
        return userService.findByUsername(username);
    }
}